<template>
  <center>
    <div class="shouj">
      <van-nav-bar
        title="订单明细"
        left-arrow
        @click-left="onClickLeft"
      />
      <div id="app">
        <span class="qu">去{{ orderList.terminal }}</span>
        <button class="h" @click="xingc">行程信息</button>
        <span class="xiao1">全程<font color="#6495ed">{{orderList.distance}}</font>公里</span><br>
        <span class="xiao2">约<font color="#6495ed">{{orderList.payconsuming}}</font>分钟</span><br><br>
        <p class="p1">请在<font color="red">10分钟</font>内到达出发点</p>
      </div>
      <div id="container"></div>
      <div class="shuju">
        <ul class="infinite-list" v-infinite-scroll="load" style="overflow:auto">
          <div class="didian">
            <span>{{form.address}}</span>
          </div>
        </ul>
      </div>
      <div class="button-group">
        <button @click="handleArrive">返回</button>
      </div>
      <div class="dd1">
        <div id="div1">
          <span id="span1">{{ this.phone }}</span>
          <span id="span2">尾号</span>
        </div>
        <div id="div2">
          <span id="span3">{{orderList.starting}}</span>
          <span id="span4">{{ orderList.destination }}</span>
        </div>
        <div id="div3">
          <span class="el-icon-chat-dot-square" @click="tochat"></span>
          <a :href="'tel:'+400456123"><i class="el-icon-phone"></i></a>
        </div>
      </div>

      <el-dialog title="请询问乘客乘车码" :visible.sync="carcodeShow">
        <el-form>
          <el-form-item label="乘车码">
            <el-input v-model="carcode"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="carcodeShow = false">取 消</el-button>
          <el-button type="primary" @click="carcodeShow = false">提 交</el-button>
        </div>
      </el-dialog>
    </div>
  </center>
</template>

<script>
import AMapLoader from "@amap/amap-jsapi-loader";
import axios from "axios";
window._AMapSecurityConfig = {
  // 安全密钥
  securityJsCode: "23a30393a855e07d2ba7621ac8e4e6d2",
};
export default {
  name: "undoneorder",
  data() {
    return {
      phone: '',
      orderList: [],
      // 地图实例
      map: null,
      toolbar: null,
      // 标记点
      marker: "",
      // 地址逆解析
      geoCoder: null,
      // 搜索提示
      AutoComplete: null,
      //缩放控件
      ToolBar:null,
      //地图放大镜
      HawkEye:null,
      // 搜索关键字
      keywords: "",
      // 位置信息
      form: {
        lng: "116.208741",
        lat: "40.136902",
        address:'',
      },
      // 搜索节流阀
      loading: false,
      // 搜索提示信息
      options: [],
      trackPositions: [],
      //精准定位
      Geolocation() {},

      driverTime: null,
      driverLong: null,
      startaddress:"",
      endaddress:"",
      // 开始的经纬度
      startLatitudeAndLongitude: "116.379028, 39.865042",
      // 结束的经纬度
      endLatitudeAndLongitude: "116.427281, 39.803719",

      // points :[
      //     { keyword: '北京市地震局（公交站）',city:'北京' }, //起始点坐标
      //     { keyword: '亦庄文化园（地铁站）',city:'北京' } //终点坐标
      //   ]
      orderId: ''
    };
  },
  created() {
    this.orderId = this.$route.params.orderId
    this.findOne()
  },
  methods: {
    tochat(){
      console.log(this.orderList)
      localStorage.setItem('phone',JSON.stringify(this.orderList.phoneNumber));
      localStorage.setItem('did',JSON.stringify(this.orderList.did));
      this.$router.push("/chat");
    },
    findOne() {
      console.log('#############')
      axios.get(`personal/queue/findOne?orderId=${this.orderId}`).then(res => {
        this.orderList = res.data
        this.phone=this.orderList.phoneNumber.slice(-4);
        console.log('**********')
        console.log(this.orderList)
      })
    },
    handleArrive(){
      this.$router.push("/dcOrders");
    },
    onClickLeft() {
      this.$router.push('/dcOrders')
    },
    async getMapApi() {

      if (this.trackPositions && this.trackPositions.length) {
        // 通过 高德 API获取 all 所有 路线 经纬度


        //构造路线导航类
        var driving = new AMap.Driving({
          map: this.map,
          panel: "panel"
        });

        var lngsrt = this.trackPositions[0];
        // 根据起终点经纬度规划驾车导航路线
        driving.search(
          new AMap.LngLat(this.trackPositions[0][0],this.trackPositions[0][1]),
          new AMap.LngLat(this.trackPositions[1][0],this.trackPositions[1][1]),
          function(status, result) {
            // result 即是对应的驾车导航信息，相关数据结构文档请参考
            // https://lbs.amap.com/api/javascript-api/reference/route-search#m_DrivingResult
            if (status === 'complete') {
              // this.map.setFitView();
              console.log('绘制驾车路线完成')
            } else {
              console.log('获取驾车数据失败：' + result)
            }
          })
      }
    },

    reset(){
      this.keywords = '';
      this.form = {
        lng: "116.208741",
        lat: "40.136902",
        address: '',
      }
      // 清除点
      this.removeMarker();
      // 标记点
      this.setMapMarker();
    },



    initMap() {
      let that = this;
      // that.trackPositions.push([this.form.lng, this.form.lat]);
      AMapLoader.load({
        // 你申请的Key
        key: "b17513b156700f5d461409278e81ffa7",
        version: "2.0",
        // 需要用到的插件
        plugins: ["AMap.Geocoder",
          "AMap.AutoComplete","" +
          "AMap.ToolBar",
          "AMap.HawkEye",
          "AMap.Geolocation",
          "AMap.Driving"
        ],
      }).then((AMap) => {


        // AMap.plugin("AMap.Driving",function (){
        //   var driving = new AMap.Driving({
        //     policy:0,
        //   });
        //   var points =[
        //     {keyword: "北京市地震局（公交站）", city: "北京"},
        //     {keyword: "亦庄文化园（地铁站）", city: "北京"}
        //   ];
        //   driving.search(points,function (status,result){

        //   });
        // }),
        that.map = new AMap.Map("container", {
          viewMode: "3D", //是否为3D地图模式
          zoom: 23, //初始化地图级别
          center: [that.form.lng, that.form.lat], //初始化地图中心点位置
        });
        //地址逆解析插件
        that.geoCoder = new AMap.Geocoder({
          city: "北京", //城市设为北京，默认：“全国”
          radius: 1000, //范围，默认：500
        });
        // 搜索提示插件
        that.AutoComplete = new AMap.AutoComplete({ city: "北京" });
        that.ToolBar =  new AMap.ToolBar({
          position: {
            bottom: '210px',
            right: '35px'
          }
        });
        that.HawkEye =  new AMap.HawkEye({
          position: {
            // bottom: '110px',
            // left: '35px'
          }
        });
        that.Geolocation = new AMap.Geolocation({
          enableHighAccuracy: true, // 是否使用高精度定位，默认：true
          timeout: 10000, // 设置定位超时时间，默认：无穷大
          offset: [10, 20],  // 定位按钮的停靠位置的偏移量
          zoomToAccuracy: true,  //  定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
          position: 'RB' //  定位按钮的排放位置,  RB表示右下

        });


        that.map.addControl(that.ToolBar);
        that.map.addControl(that.HawkEye);
        that.map.addControl(that.Geolocation);

        // this.map.addControl(toolbar);
        // 清除点
        that.removeMarker();
        // 标记点
        that.setMapMarker();
        //点击获取经纬度;
        that.map.on("click", (e) => {
          // 获取经纬度
          that.form.lng = e.lnglat.lng;
          that.form.lat = e.lnglat.lat;
          // 清除点
          that.removeMarker();
          // 标记点
          that.setMapMarker();


        });
      })
        .catch((err) => {
          // 错误
        });
    },
    // 标记点
    setMapMarker() {
      // 自动适应显示想显示的范围区域
      this.map.setFitView();
      this.marker = new AMap.Marker({
        map: this.map,
        position: [this.form.lng, this.form.lat],
      });
      // 逆解析地址
      this.toGeoCoder();
      this.map.setFitView();
      this.map.add(this.marker);
    },
    // 清除点
    removeMarker() {
      if (this.marker) {
        this.map.remove(this.marker);
      }
    },
    xingc(){
      this.$router.push("/Trip")
    },
    // 逆解析地址
    toGeoCoder() {
      let lnglat = [this.form.lng, this.form.lat];
      this.geoCoder.getAddress(lnglat, (status, result) => {
        if (status === "complete") {
          this.form.address = result.regeocode.formattedAddress;
        }else {
          this.form.address = '';
        }
      });
    },
    querySearchAsync(queryString, cb){
      if (queryString) {
        this.AutoComplete.search(queryString, (status, result) => {
          if(status === 'complete'){
            this.options = result.tips;
            for (let i = 0; i < this.options.length; i++) {
              this.options[i].value = this.options[i].name;
            }
          }else {
            this.options = [];
          }
          cb(this.options);
        });
      } else {
        this.options = [];
        cb(this.options);
      }
    },
    handleSelect(val){
      // 清空时不执行后面代码
      if (!val) {
        return;
      }
      this.trackPositions = [] ;
      this.trackPositions.push([this.form.lng, this.form.lat]);
      this.form = {
        lng: val.location.lng,
        lat: val.location.lat,
      };
      this.trackPositions.push([this.form.lng,this.form.lat])
      // 清除点
      this.removeMarker();
      // 标记点
      this.setMapMarker();

      // 绘画地图
      this.getMapApi(this.trackPositions);

    },
  },
  mounted() {
    this.initMap();

  },
  unmounted() {
    this.map.destroy();
  },

};
</script>

<style scoped>
.h {
  position: relative;
  top: -20px;
  right: 20px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  width: 100px;
  height: 35px;
  float: right;
}

.shouj {
  margin-top: 100px;
}

#container {
  width: 400px;
  height: 500px;
}

.shuju {
  width: 380px;
  height: 80px;
  background-color: #c8e3e3;
  margin-left: 10px;
  float: left;
}

.didian {
  width: 300px;
  height: 60px;
  background-color: #eef4ff;
}

ul {
  width: 300px;
  height: 80px;
  margin: 0 auto;
}

.button-group {
  /* 按钮组的样式，可以根据实际需求调整 */

  button {
    width: 130px;
    height: 40px;
    border-radius: 20px;
  }
}

button {
  padding: 8px 16px;
  background-color: #FF9959;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  /* 按钮的样式，可以根据实际需求调整 */
}

button:hover {
  background-color: #4EA9CB;
  /* 按钮悬停时的样式，可以根据实际需求调整 */
}

#app {
  height: 120px;
  width: 400px;
  margin-top: 30px;

  p {
    float: left;
  }
}

.qu {
  position: relative;
  top: -20px;
  left: 10px;
  float: left;
  font-size: 25px;
}

.xiao1 {
  position: relative;
  top: 15px;
  left: -210px;
}

.xiao2 {
  position: relative;
  top: -5px;
  left: -130px;
}

.p1 {
  position: relative;
  top: -60px;
  left: 200px;
}

.dd1 {
  position: relative;
  top: -700px;
  width: 400px;
  height: 80px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
  #div1 {
    text-align: left;
    float: left;
    width: 25%;
    height: 80px;
    #span1 {
      display: block;
      margin-top: 15px;
      font-size: 25px;
    }
  }
  #div2 {
    text-align: left;
    float: left;
    width: 50%;
    height: 80px;
    #span3 {
      color: black;
      text-align: left;
      margin-left: 10px;
      display: block;
      margin-top: 15px;
      font-size: 18px;
    }
    #span4 {
      margin-left: 10px;
      display: block;
      margin-top: 2px;
      font-size: 18px;
    }
  }
  #div3 {
    float: left;
    width: 25%;
    height: 80px;
  }
}

.ff1 {
  position: relative;
  top: 12px;
  left: 10px;
  font-size: 24px;
}

.ff2 {
  position: relative;
  top: 30px;
  left: -33px;
}

.el-icon-phone {
  position: relative;
  top: 30%;
  font-size: 30px;
}

.el-icon-chat-dot-square {
  position: relative;
  top: 25px;
  font-size: 30px;
}

.ff3 {
  position: relative;
  top: 8px;
  left: 20px;
}

.ff4 {
  position: relative;
  top: 28px;
  left: -110px;
}
</style>
